package test_try;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class TestXml {

    public static void main(String[] args) throws Exception {
        //创建解析
        SAXReader reader = new SAXReader();
        // 2.创建文件
        Document document = reader.read("day14-code/src/test_try/xml.xml");
        //输出根元素标签
        Element rootElement = document.getRootElement();
        System.out.println("根元素标签为："+rootElement.getName());
        // 3.获取所有子元素标签以及属性
        rootElement.elements().forEach(element -> {  //遍历所有子元素标签
            System.out.println("\t子元素标签为："+element.getName()+"，属性为："+element.attributeValue("category"));//获取属性

            Element title = element.element("title");//获取孙子元素标签
            System.out.println("\t\t孙子元素标签为："+title.getName()+"\n\t\t标题是："+title.getText());

            Element author = element.element("author");//获取孙子元素标签
            System.out.println("\t\t孙子元素标签为："+author.getName()+"\n\t\t作者是："+author.getText());

            Element year = element.element("year");//获取孙子元素标签
            System.out.println("\t\t孙子元素标签为："+year.getName()+"\n\t\t年份是："+year.getText());

            Element price = element.element("price");//获取孙子元素标签
            System.out.println("\t\t孙子元素标签为："+price.getName()+"\n\t\t价格是："+price.getText());
        });
    }
}
